﻿using Dapper;
using HrEFCore;
using HrEFCore.Models;
using HrIService;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HrService
{
	public class MCService:IMCService
	{
		private readonly MyDbContext myDbContext;

		public MCService(MyDbContext myDbContext)
        {
			this.myDbContext = myDbContext;
		}

		public async Task<List<CM>> CMLDAsync(string id)
		{
			return await myDbContext.CM.Where(e=>e.MajorKindId == id).ToListAsync();
		}

		public async Task<int> DDHFAsync(string id)//调动次数的修改
		{
			var hf =await myDbContext.HF.FirstOrDefaultAsync(e=>e.HumanId == id);
			if (hf == null)
			{
				return 0;
			}
			hf.MajorChangeAmount += 1;
			myDbContext.Update(hf);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<int> HFUpd(HF hf)
		{
			var entity = myDbContext.HF.Attach(hf);
			entity.Property(e => e.FirstKindId).IsModified = true;
			entity.Property(e => e.FirstKindName).IsModified = true;
			entity.Property(e => e.SecondKindId).IsModified = true;
			entity.Property(e => e.SecondKindName).IsModified = true;
			entity.Property(e => e.ThirdKindId).IsModified = true;
			entity.Property(e => e.ThirdKindName).IsModified = true;
			entity.Property(e => e.HumanMajorKindId).IsModified = true;
			entity.Property(e => e.HumanMajorKindName).IsModified = true;
			entity.Property(e => e.HumanMajorId).IsModified = true;
			entity.Property(e => e.HunmaMajorName).IsModified = true;
			entity.Property(e => e.SalaryStandardId).IsModified = true;
			entity.Property(e => e.SalaryStandardName).IsModified = true;
			entity.Property(e => e.SalarySum).IsModified = true;//总额
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<int> HFUpdate(HF hf,string humanId)
		{
			var existingHF = myDbContext.HF.Local.FirstOrDefault(e => e.HumanId == humanId);
			if (existingHF != null)
			{
				existingHF.FirstKindId = hf.FirstKindId;
				existingHF.FirstKindName = hf.FirstKindName;
				existingHF.SecondKindId = hf.SecondKindId;
				existingHF.SecondKindName = hf.SecondKindName;
				existingHF.ThirdKindId = hf.ThirdKindId;
				existingHF.ThirdKindName = hf.ThirdKindName;
				existingHF.HumanMajorKindId = hf.HumanMajorKindId;
				existingHF.HumanMajorKindName = hf.HumanMajorKindName;
				existingHF.HumanMajorId = hf.HumanMajorId;
				existingHF.HunmaMajorName = hf.HunmaMajorName;
				existingHF.SalaryStandardId = hf.SalaryStandardId;
				existingHF.SalaryStandardName = hf.SalaryStandardName;
				existingHF.SalarySum = hf.SalarySum;

				return await myDbContext.SaveChangesAsync();
			}
			else
			{
				return 0; // 实体对象未找到
			}
		}

		public async Task<int> MCAddAsync(MC mc)//调动添加
		{
			myDbContext.MC.AddAsync(mc);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<SS> SelectByIdFromSS(string id)
		{
			return await myDbContext.SS.FirstOrDefaultAsync(e=>e.StandardId == id);
		}

		public async Task<FenYe2<MC>> SelectFYMC(int CurrentPage, int PageSize, string where)
		{
			using (var conn = myDbContext.Database.GetDbConnection())
			{
				DynamicParameters dp = new DynamicParameters();
				dp.Add("@pageSize", PageSize);
				dp.Add("@keyName", "MchId");
				dp.Add("@tableName", "MC");
				dp.Add("@where", where);
				dp.Add("@currentPage", CurrentPage);
				dp.Add("@lie", "*");
				dp.Add("@rows", direction: ParameterDirection.Output, dbType: DbType.Int32);
				string sql = "exec [dbo].[proc_FenYe2] @pageSize,@keyName,@tableName,@where,@currentPage,@lie,@rows out";
				IEnumerable<MC> list = await conn.QueryAsync<MC>(sql, dp);
				int rows = dp.Get<int>("rows");
				FenYe2<MC> fy = new FenYe2<MC>()
				{
					List = list,
					Rows = rows
				};
				return fy;
			}
		}

		public async Task<FenYe2<MC>> SelectMCByFYAsync(FenYe2<MC> fenYe)//分页查询
		{
			using (var conn = myDbContext.Database.GetDbConnection())
			{
				DynamicParameters dp = new DynamicParameters();
				dp.Add("@currentPage", fenYe.CurrentPage);
				dp.Add("@pageSize", fenYe.PageSize);
				dp.Add("@keyName", "MchId");
				dp.Add("@tableName", "MC");
				dp.Add("@rows", direction: ParameterDirection.Output, dbType: DbType.Int32);
				string sql = "exec [dbo].[proc_FenYe] @currentPage,@pageSize,@keyName,@tableName,@rows out";
				fenYe.List = await conn.QueryAsync<MC>(sql,dp);
				fenYe.Rows = dp.Get<int>("rows");
				return fenYe;
			}
		}

		public Task<MC> SelectMCById(short id)
		{
			return myDbContext.MC.FirstOrDefaultAsync(e=>e.MchId == id);
		}

		public async Task<int> SHPass(MC mc)//审核通过
		{
			var entity = myDbContext.MC.Attach(mc);
			entity.Property(e=>e.Checker).IsModified = true;
			entity.Property(e=>e.CheckReason).IsModified = true;
			entity.Property(e=>e.CheckStatus).IsModified = true;
			entity.Property(e=>e.CheckTime).IsModified = true;
			return await myDbContext.SaveChangesAsync();
		}
	}
}
